<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>LVS的DR设置 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>LVS的DR设置</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LVS/">LVS</a>
			</span>
		
	</div>

	

	
		<h2 id="1-环境说明"><a href="#1-环境说明" class="headerlink" title="1.环境说明"></a>1.环境说明</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;三台机器</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;director  （eth0： 192.168.0.67， vip eth0:0： 192.168.0.64）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;real server1 （eth0 rip：192.168.0.66 ，vip lo:0：192.168.0.64）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;real server2 （eth0 rip：192.168.0.65 ，vip lo:0：192.168.0.64）</p>
<h2 id="2-编写脚本"><a href="#2-编写脚本" class="headerlink" title="2.编写脚本"></a>2.编写脚本</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;director 上执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@dir ~]<span class="comment"># vim /usr/local/sbin/lvs_dr.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line"><span class="built_in">echo</span> 1 &gt; /proc/sys/net/ipv4/ip_forward</div><div class="line">ipv=/sbin/ipvsadm</div><div class="line">vip=192.168.0.64</div><div class="line">rs1=192.168.0.66</div><div class="line">rs2=192.168.0.65</div><div class="line">ifconfig eth0:0 <span class="variable">$vip</span> broadcast <span class="variable">$vip</span> netmask 255.255.255.255 up</div><div class="line">route add -host <span class="variable">$vip</span> dev eth0:0</div><div class="line"><span class="variable">$ipv</span> -C</div><div class="line"><span class="variable">$ipv</span> -A -t <span class="variable">$vip</span>:80 -s rr </div><div class="line"><span class="variable">$ipv</span> -a -t <span class="variable">$vip</span>:80 -r <span class="variable">$rs1</span>:80 -g -w 1</div><div class="line"><span class="variable">$ipv</span> -a -t <span class="variable">$vip</span>:80 -r <span class="variable">$rs2</span>:80 -g -w 1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后在两台 rs 上执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /usr/<span class="built_in">local</span>/sbin/lvs_dr_rs.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line">vip=192.168.0.64</div><div class="line">ifconfig lo:0 <span class="variable">$vip</span> broadcast <span class="variable">$vip</span> netmask 255.255.255.255 up </div><div class="line">route add -host <span class="variable">$vip</span> lo:0</div><div class="line"><span class="built_in">echo</span> <span class="string">"1"</span> &gt;/proc/sys/net/ipv4/conf/lo/arp_ignore</div><div class="line"><span class="built_in">echo</span> <span class="string">"2"</span> &gt;/proc/sys/net/ipv4/conf/lo/arp_announce</div><div class="line"><span class="built_in">echo</span> <span class="string">"1"</span> &gt;/proc/sys/net/ipv4/conf/all/arp_ignore</div><div class="line"><span class="built_in">echo</span> <span class="string">"2"</span> &gt;/proc/sys/net/ipv4/conf/all/arp_announce</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关于 arp_ignore 和 arp_announce 参考：<a href="https://hcldirgit.github.io/2017/08/07/15.%20LVS%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E4%B8%ADarp_ignore%E5%92%8Carp_annonuce%E5%8F%82%E6%95%B0%E9%85%8D%E7%BD%AE%E7%9A%84%E5%90%AB%E4%B9%89/" target="_blank" rel="external">LVS负载均衡中arp_ignore和arp_annonuce参数配置的含义</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后 driector 上执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@dir ~]<span class="comment"># sh /usr/local/sbin/lvs_dr.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两台 rs 上执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sh /usr/<span class="built_in">local</span>/sbin/lvs_dr_rs.sh</div></pre></td></tr></table></figure>
<h2 id="3-测试"><a href="#3-测试" class="headerlink" title="3.测试"></a>3.测试</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;windows 下浏览器测试访问 <a href="http://192.168.0.64" target="_blank" rel="external">http://192.168.0.64</a></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84DR%E8%AE%BE%E7%BD%AE/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更换浏览器</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84DR%E8%AE%BE%E7%BD%AE/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者换台计算机用 curl</p>
<p> <figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84DR%E8%AE%BE%E7%BD%AE/02.png?raw=true" alt=""></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/LVS/16. LVS的DR设置/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
